package com.gy.yemall.yemallproduct.service.impl;

import com.fasterxml.jackson.databind.annotation.JsonAppend;
import com.gy.yemall.yemallproduct.dao.AttrDao;
import com.gy.yemall.yemallproduct.entity.AttrEntity;
import com.gy.yemall.yemallproduct.entity.vo.AttrGroupRelationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gy.common.utils.PageUtils;
import com.gy.common.utils.Query;

import com.gy.yemall.yemallproduct.dao.AttrAttrgroupRelationDao;
import com.gy.yemall.yemallproduct.entity.AttrAttrgroupRelationEntity;
import com.gy.yemall.yemallproduct.service.AttrAttrgroupRelationService;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Autowired
    private AttrDao attrDao;

    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<AttrEntity> queryAtrrByGroupId(Integer attrgroupId) {
        // 根据attrgroupId查询关联表
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntity = baseMapper.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>()
                .eq("attr_group_id", attrgroupId));
        List<AttrEntity> list = new ArrayList<>();
        attrAttrgroupRelationEntity.forEach((item) -> {
            list.add(attrDao.selectById(item.getAttrId()));
        });
        return list;
    }

    @Override
    public void saveRalation(AttrGroupRelationVo[] attrGroupRelationVo) {
        // attrAttrgroupRelationDao.saveRalation(attrGroupRelationVo[0].getAttrId(),attrGroupRelationVo[0].getAttrGroupId());
        List<AttrAttrgroupRelationEntity> entities = Arrays.asList(attrGroupRelationVo).stream().map((item) -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());

        entities.forEach((item) -> {
            attrAttrgroupRelationDao.insert(item);
        });

    }

}